package com.william.jdbcplus.demo.controller;

import com.william.jdbcplus.boot.starter.util.DaoUtil;
import com.william.jdbcplus.boot.starter.wrap.QueryLambdaBootWrap;
import com.william.jdbcplus.common.page.IPage;
import com.william.jdbcplus.common.page.Page;
import com.william.jdbcplus.core.ds.DataSourceContextHolder;
import com.william.jdbcplus.core.query.EntityWrap;
import com.william.jdbcplus.demo.constant.DsConst;
import com.william.jdbcplus.demo.entity.HisRecord;
import com.william.jdbcplus.demo.entity.OrderOutDlxy;
import com.william.jdbcplus.demo.entity.Result;
import com.william.jdbcplus.demo.entity.Test;
import com.william.jdbcplus.demo.vo.MapVO;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "测试")
@RestController
@AllArgsConstructor
@Validated
@RequestMapping("/test")
@Slf4j
public class TestController {
    @GetMapping(value = "/exec")
    public String exec() {
        DataSourceContextHolder.set(DsConst.HIS);
        QueryLambdaBootWrap lambdaBootstrap = new QueryLambdaBootWrap();
        lambdaBootstrap
                .selectAs(OrderOutDlxy::getOrdRowID, MapVO::getKey)
                .selectAs(OrderOutDlxy::getSfdjh, MapVO::getValue)
                .from(OrderOutDlxy.class);

        List<MapVO> list = lambdaBootstrap.queryList(MapVO.class);
        Map<String, String> mapOut = new HashMap<>();
        for (MapVO vo : list) {
            if (vo != null && vo.getKey() != null) {
                mapOut.put(vo.getKey(), vo.getValue());
            }
        }

        DataSourceContextHolder.remove();

        lambdaBootstrap.clear();
        lambdaBootstrap
                .selectCount(Test::getId)
                .from(Test.class);

        EntityWrap<Test> wrap = new EntityWrap<>(Test.class);
        wrap.lambda().between(Test::getAuditTime, getDate("2019-01-01"), getDate("2020-12-31"));
        Integer count = DaoUtil.BASE.count(wrap);
        int pageSize = 1000;
        int pageTotal = (count + pageSize - 1) / pageSize;
        wrap.lambda().orderByAsc(Test::getId);
        for (int pageIndex = 1; pageIndex <= pageTotal; pageIndex++) {
            IPage<?> pageParam = new Page(pageIndex, pageSize);
            IPage<Test> page = DaoUtil.BASE.page(pageParam, wrap);
            List<Test> listTest = page.getRecords();
            List<HisRecord> recordList = new LinkedList<>();
            for (Test test : listTest) {
                if (test == null || test.getId() == null) {
                    continue;
                }
                EntityWrap<Result> wrapResult = new EntityWrap<>(Result.class);
                wrapResult.lambda().eq(Result::getTestId, test.getId());
                List<Result> resultList = DaoUtil.BASE.list(wrapResult);
                for (Result result : resultList) {
                    HisRecord record = convertInRecord(test, result, mapOut);
                    recordList.add(record);
                }
            }

            DaoUtil.BASE.addList(recordList);
        }


        return "成功";
    }

    private HisRecord convertInRecord(Test test, Result result, Map<String, String> mapOut) {
        HisRecord record = new HisRecord();
        if (test.getPtype() != null && test.getPtype().equals("1")) {
            record.hisId = mapOut.get(test.getOrderPatId());
            record.visiType = "住院";
        } else {
            record.hisId = test.getOrderPatId();
        }

        record.examId = result.getItemCode();
        record.examName = result.getItemNameCn();
        record.executeDeptName = test.getDepartName();
        record.orderDeptName = test.getDepartName();
        record.patientId = test.getPatMedicalRecNo();
        record.patientName = test.getPatName();
        record.packageChargeId = result.getOrderItemCode();
        record.packageChargeName = result.getOrderItemName();
        record.recieveDate = test.getWriteTime();
        record.reportDate = test.getAuditTime();
        record.socialCardId = test.getPatMedicalRecNo();
        record.examResult = result.getItemResultText() + result.getItemUnit();
        record.hospCode = test.getHospCode();
        record.hospName = test.getHospName();
        record.dstHospCode = test.getDstHospCode();
        record.dstHospName = test.getDstHospName();
        return record;
    }

    private Date getDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date d = null;
        //这里会有一个异常，所以要用try catch捕获异常
        try {
            d = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }

}
